package com.codejiwei.core.rdd

import org.apache.spark.{SparkConf, SparkContext}

object Spark_Operator_Flatmap {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[1]").setAppName("FlatMap")
    val sc = new SparkContext(conf)

    val rdd = sc.makeRDD(List("Hello Scala", "Hello Spark"))

    val rdd1 = rdd.flatMap(_.split(" "))
    println(rdd1.collect().mkString(", "))


    //模式匹配如果不是List[_]那么就转成List
    val rdd2 = sc.makeRDD(List(List(1, 2, 3), 3, List(4, 5), "Tom"))
    val rdd3 = rdd2.flatMap {
      case list: List[_] => list
      case l => List(l)
    }

//    rdd3.collect().foreach(println)
    println(rdd3.collect().mkString(", "))

    sc.stop()
  }
}
